define("core/map", [
    "leaflet",
    "core/baseobject",
    "core/namespace",
    "leaflet/chartLayer",
    "leaflet/googleLayer",
    "leaflet/scalefactor",
    "control/draw",
    "control/measure",
    "control/layerswitch",
    "data/ajax",
    "plugins/contextmenu",
    "func/realTarget",
    "func/portInfo",
    "func/measureedit",
    "func/quicklocate"
], function(L) {

    /*地图类*/
    L.ICT.Map = L.ICT.BaseObject.extend({

        //地图对象
        map: null,

        //底图
        _baseLayer: null,

        //地图绘制
        _drawTool: null,

        //实时目标类对象
        realtarget: null,

        //港口信息
        portlayer: null,

        //标牌显示的类型
        labelType: null, // 值有‘shipname’ ，‘id’

        //地图操作状态
        OperateState: {
            tshf: false, //态势回放
            hjcx: false, //航迹查询
            // port: false, //港口信息
            weather: false,
            wjfx: false, //挖掘分析
            wjfx_hdyc: false, //活动预测
            jktj: false,
            wxpg:false //威胁评估

        },

        initialize: function(options) {
            L.setOptions(this, options);

            // var center = options.baseLayer.center,
            //     zoom = options.baseLayer.zoom,
            //     minZoom = options.baseLayer.minZoom,
            //     maxZoom = options.baseLayer.maxZoom;

            /*ww*/
            var center = options.MapOptions.center,
    			    zoom = options.MapOptions.zoom,
    			    minZoom = options.MapOptions.minZoom,
    			    maxZoom = options.MapOptions.maxZoom;

            this.ajax = new L.ICT.Ajax();
            this._allcount = '--';

            //地图容器大小
            this._initMapStyle();

            //地图对象
            var map = new L.Map('mappanel', {
                center: new L.latLng(center),
                zoom: zoom,
                minZoom: minZoom,
                maxZoom: maxZoom,
                crs: L.CRS.EPSG3395,
                zoomControl: false,
                attributionControl: false,
                closePopupOnClick: false,
                doubleClickZoom: false,
                // worldCopyJump:true,
                maxBounds:L.latLngBounds(L.latLng(-85,-540),L.latLng(85,540))
            });

            //底图图层-old没有卫星图
            // var baseLayer;
            // if (options.IsUseNetWorkCharMap) {
            //     baseLayer = new L.tileLayer.ChartLayer2(options.ChartLayerUrl2, {
            //         id: options.baseLayer.id,
            //         errorTileUrl: './themes/images/errorimg.png'
            //     });
            // } else {
            //     baseLayer = new L.tileLayer.ChartLayer(options.ChartLayerUrl, {
            //         id: options.baseLayer.id,
            //         errorTileUrl: './themes/images/errorimg.png'
            //     });
            // }
            // map.addLayer(baseLayer);
            // this.map = map;

            //底图切换ww
           for(var i=0;i<this.options.changeLayers.length;i++){
               var obj = this.options.changeLayers[i];
               switch (obj.id){
                   case "baseLayer-ocean":
                        // obj.layer = L.tileLayer.ChartLayer(null,{id:obj.id,opacity:0});
                        if (options.IsUseNetWorkCharMap) {
                            obj.layer = new L.tileLayer.ChartLayer2(options.ChartLayerUrl2, {
                                id: obj.id,
                                opacity:0,
                                errorTileUrl: './themes/images/errorimg.png'
                            });
                        } else {
                            obj.layer = new L.tileLayer.ChartLayer(options.ChartLayerUrl, {
                                id: obj.id,
                                opacity:0,
                                errorTileUrl: './themes/images/errorimg.png'
                            });
                        };
                        break;
                   case "baseLayer-googleRoadMap":
                        obj.layer = L.tileLayer.GoogleLayer(null,{id:obj.id,lyrs:'m',opacity:0});// m地图
                        break;
                   case "baseLayer-gooleSatellite":
                        obj.layer = L.tileLayer.GoogleLayer(null,{id:obj.id,lyrs:'y',opacity:0});// y带标签的卫星图
                        break;
                   default: ;
               }
               map.addLayer(obj.layer);
               if(obj.active){
                   this._baseLayer = obj.layer;
               }
            }

           if(this._baseLayer){
               this._baseLayer.setOpacity(1);
            }
           this.map = map;

            //地图控件
            // new L.ICT.Control.LayerSwitch()
            //    .addTo(this.map)
            //    .on("baseLayerChangeEvent",function(e){
            //        this.setBaseLayer(e.curBaseLayer);
            //        this.portlayer.reloadPortLayer();
            //     },this);
            new L.ICT.Control.LayerSwitch()
                 .addTo(this.map)
                 .on("baseLayerChangeEvent",function(e){
                     var layer = e.curBaseLayer;
                     var oldlayer =  this.getBaseLayer();
                     this.setBaseLayer(e.curBaseLayer);
                     //google切换到海图
                     if(layer.options.id === 'baseLayer-ocean'){
                         var center = this.map.getCenter();
                         this.map.options.crs = L.CRS.EPSG3395;
                         this.map.panTo(center);
                         this.realtarget.reloadTargets();
                         this.portlayer.reloadPortLayer();

                     }  else if(oldlayer.options.id === 'baseLayer-ocean'){
                        //海图切换到google
                         var center = this.map.getCenter();
                         this.map.options.crs = L.CRS.EPSG3857;
                         this.map.panTo(center);
                         this.realtarget.reloadTargets();
                         this.portlayer.reloadPortLayer();
                     }

                  },this);

            L.control.zoom({ position: 'topleft' }).addTo(this.map);
            // L.control.scale({ position: 'bottomleft',imperial:true }).addTo(this.map);
            L.control.scalefactor({ position: 'bottomleft' }).addTo(this.map);
            this._attrcontrol = L.control.attribution({ position: 'bottomright', prefix: false }).addTo(this.map);
            this._targetcountcontrol = L.control.attribution({ position: 'bottomright', prefix: false }).addTo(this.map);
            // this._scalecontrol = L.scontrol.attribution({ position: 'bottomleft', prefix: false }).addTo(this.map);

            //地图事件
            this.map.on("movestart", this._movestartEvt, this)
                .on("mousemove", this._mousemoveEvt, this)
                .on("moveend", this._moveendEvt, this)
                .on("contextmenu", this._mapContextMenuEvt, this);

            // 港口图层加载
            this.portlayer = new L.ICT.PortInfo(this);

            //实时目标
            this.realtarget = new L.ICT.RealTarget(this);
            this.realtarget.addShipDistLayer(); // 添加绿点图
            // this.realtarget.addRealTargetLayer();
            if(this.getCurZoom() >= this.realtarget.config.showLevel) {
              this.realtarget.getRealTarget();
            }

            this.updateCountInterval();

            //初始化目标右键菜单
            this.realtarget.initContextMenu();
            this._initMapContextMenu();

            //地图量测
            this.measureTool = new L.ICT.Measure.Length(this.map, null);

            //窗口变化时地图大小变化
            $(window).resize(function() {
                this._initMapStyle();
                this.map.invalidateSize(false);
            }.bind(this));
        },

        _initMapStyle: function() {
            var bodyHeight = $('.container-fluid').outerHeight();
            var topHeight = $('#toppanel').outerHeight();
            var height = bodyHeight - topHeight;
            var width = $('.container-fluid').outerWidth();
            $('#mappanel').height(height);
            $('#mappanel').width(width);
        },

        _initMapContextMenu: function() {
            var self = this;
            $.contextMenu({
                selector: '#mappanel',
                trigger: 'none',
                zIndex: 99999,
                className: 'ict_contextmenu_map',
                callback: function(key, options) {
                    self._rightClickCallback(key, options);
                },
                items: {
                    "bpxs": {
                        "name": "标牌显示",
                        // "icon":
                        "items": {
                            "bpxs-shipname": { "name": "船名" },
                            "bpxs-ph": { "name": "批号" },
                            "bpxs-close": { "name": "关闭显示" }
                        }
                    },
                    "hqdw": {
                        "name": "海区定位",
                        "items": {
                            "hqdw-key1": { "name": "东黄海区" },
                            "hqdw-key2": { "name": "东南沿海海区" },
                            "hqdw-key3": { "name": "南海北部海区" },
                            "hqdw-key4": { "name": "南沙海区" }
                        }
                    },
                    "jlls": { "name": "距离量算(手绘)" },
                    "jlls_bj": { "name": "距离量算(编辑)" },
                    "qykc": { "name": "区域开窗" },
                    "quicklocate": { "name": "快速定位" }
                }
            });
        },

        _rightClickCallback: function(key, options) {
            switch (key) {
                case "bpxs-shipname":
                    this.realtarget.labelAllShip('shipname');
                    this.labelType = 'shipname';
                    break;
                case "bpxs-ph":
                    this.realtarget.labelAllShip('id');
                    this.labelType = 'id';
                    break;
                case "bpxs-close":
                    this.realtarget.hideAlllabel();
                    this.labelType = null;
                    break;
                case "hqdw-key1":
                    this.locateToArea('DH');
                    break;
                case "hqdw-key2":
                    this.locateToArea('DN');
                    break;
                case "hqdw-key3":
                    this.locateToArea('NH');
                    break;
                case "hqdw-key4":
                    this.locateToArea('NS');
                    break;
                case "jlls":
                    this.measureTool.disable();
                    this.measureTool.enable();
                    break;
                case "jlls_bj":
                    L.ICT.Func["MeasureEdit"].run();
                    break;
                case "qykc":
                    this.realtarget.openWindowArea(this.realtarget.currentTarget && this.realtarget.currentTarget.options.data);
                    break;
                case "quicklocate":
                    L.ICT.Func["QuickLocate"].run();
                    break;
                default:
                    ;
            }
        },

        stopInterval: function() {
            if (this._interval) {
                window.clearInterval(this._interval);
                this._interval = null;
            }
        },

        updateCountInterval: function() {
            this.stopInterval();
            this.updateCount(true);
            this._interval = window.setInterval(function() {
                this.updateCount(true);
            }.bind(this), 3 * 60 * 1000);
        },

        updateCount: function(isUpdateAllCount) {
            isUpdateAllCount = !!isUpdateAllCount;
            var $container = $(this._targetcountcontrol.getContainer());
            var html = '<span>';
            html += '总目标数：';
            html += this._allcount;
            html += '&nbsp&nbsp';
            html += '当前目标数：';
            html += this.realtarget.getTargetCount();
            $container.html(html);
            if (isUpdateAllCount) {
                var url = this.realtarget.config.targetCountUrl;
                var data = {
                    mode: Project_ParamConfig.CurrentMode
                };
                this.ajax.post(url, data, true, this, function(res, error) {
                    if (error) {
                        return;
                    }
                    if (res.state !== 1) {
                        return;
                    }
                    this._allcount = res.msg.count;
                    var crcount = this.realtarget.getTargetCount();
                    var html = '<span>';
                    html += '总目标数：';
                    html += this._allcount;
                    html += '&nbsp&nbsp';
                    html += '当前目标数：';
                    html += crcount;
                    $container.html(html);
                });
            }
        },

        _updateScale: function() {
            var $container = $(this._scalecontrol.getContainer());
            var html = '';
            var map = this.map;
            var onemeterTopx = 2835;
            var y = map.getSize().y / 2;

            var maxMeters = map.distance(
                map.layerPointToLatLng([0, 0]),
                map.layerPointToLatLng([100, 0]));
            var round = Math.round(maxMeters); //L.ict.app.util.tool.getRoundNum(maxMeters);
            html += '比例尺 1:' + round;
            $container.html(html);
        },

        _mousemoveEvt: function(e) {
            //更新坐标
            var latlng = e.latlng;
            if (latlng.lng > 180) {
                latlng.lng = latlng.lng - 360;
            } else if (latlng.lng < -180) {
                latlng.lng = latlng.lng + 360;
            }
            var lat = L.ict.app.util.tool.latlngTodfmStr(latlng.lat, 'lat');
            var lng = L.ict.app.util.tool.latlngTodfmStr(latlng.lng, 'lng');
            var html = [];
            html.push('<span>经度：' + lng + '</span><span>&nbsp&nbsp纬度：' + lat + '</span>');
            html = html.join('');
            if (this._attrcontrol) {
                var container = this._attrcontrol.getContainer();
                $(container).html(html);
            }
            //更新当前目标数
            this.updateCount(false);
        },

        _movestartEvt: function(e) {
            this._moverstartBounds = e.target.getBounds();
            this._startzoom = e.target.getZoom();
            this._startcenter = e.target.getCenter();
        },

        _moveendEvt: function(e) {
            if (this.hasPoppanel()) {
                return;
            }
            if (this.OperateState.tshf) {
                return;
            }
            if (this.OperateState.weather) {
                return;
            }
            // if(this.OperateState.hjcx){return;}
            // if(this.OperateState.port){return;}
            if (this.OperateState.wjfx) {
                return;
            }
            if (this.OperateState.wjfx_hdyc) {
                return;
            }
            if (this.OperateState.jktj) {
                return;
            }
            if (this.OperateState.wxpg){
              return;
            }

            this._moveendBounds = e.target.getBounds();
            this._endzoom = e.target.getZoom();
            this._endcenter = e.target.getCenter();

            this._moverstartBoundsExtend = this._moverstartBounds && this._moverstartBounds.pad(this.realtarget.config.bufferRatio);
            this._moveendBoundsExtend = this._moveendBounds.pad(this.realtarget.config.bufferRatio);

            this._startzoom = this._startzoom || this.map.getZoom();
            this._startcenter = this._startcenter || this.map.getCenter();

            // 港口按层级加载
            this.portlayer.showOrHidePortLayer();

            //范围无变化
            if (this._endzoom === this._startzoom && this._startcenter.equals(this._endcenter)) {

            }
            //范围平移
            else if (this._endzoom === this._startzoom && !this._startcenter.equals(this._endcenter)) {
                if (this._endzoom >= this.realtarget.config.showLevel) {
                    //相交
                    if (this._moveendBoundsExtend.intersects(this._moverstartBoundsExtend)) {
                        this.realtarget.getRealTarget();
                    } else { //相离
                        this.realtarget.getRealTarget();
                    }

                }
            }
            //范围缩放
            else {
                if (this._startzoom < this.realtarget.config.showLevel && this._endzoom < this.realtarget.config.showLevel) {
                    //缩放前后都是绿点图
                    return;

                } else if (this._startzoom >= this.realtarget.config.showLevel && this._endzoom >= this.realtarget.config.showLevel) {
                    //缩放前后都是实时船舶图
                    //范围放大
                    if (this._endzoom > this._startzoom) {
                        this.realtarget.getRealTarget();
                    }
                    //范围缩小
                    if (this._endzoom < this._startzoom) {
                        this.realtarget.getRealTarget();
                    }

                } else if (this._startzoom < this.realtarget.config.showLevel && this._endzoom >= this.realtarget.config.showLevel) {
                    //缩放前是绿点图，缩放后是实时图

                    // this.realtarget.hideShipDistLayer();
                    this.realtarget.showRealTargetLayer();
                    this.realtarget.getRealTarget();

                } else if (this._startzoom >= this.realtarget.config.showLevel && this._endzoom < this.realtarget.config.showLevel) {
                    //缩放前是实时图，缩放后是绿点图
                    this.realtarget.hideRealTargetLayer();
                    // this.realtarget.showShipDistLayer();
                }
            }
        },

        _mapContextMenuEvt: function(e) {
            // console.log("contextmenu");
            var mapcontainer = this.map.getContainer();
            var x = e.containerPoint.x + mapcontainer.offsetLeft;
            var y = e.containerPoint.y + mapcontainer.offsetTop;
            //调出右键菜单
            if ($('.leaflet-marker-rightclick-icon').length > 0) {
                $('.leaflet-marker-rightclick-icon').contextMenu(false);
            }
            $('#mappanel').contextMenu(true);
            $('#mappanel').contextMenu({ x: x, y: y });
        },

        /**
         *激活鼠标状态
         *@method activate
         *@param type {string} 鼠标状态类型
         *@param callback {Object}  回调函数
         *@param precall {Object} 激活后执行函数
         *@param context {Object} 当前上下文
         */
        activate: function(type, callback, precall, context) {
            this.deactivate();
            this.setCursor(type);
            switch (type) {
                case L.ICT.MapMouseState.PAN:
                    ;
                    break;

                case L.ICT.MapMouseState.ZOOM_IN:
                    this.map.zoomIn();
                    break;

                case L.ICT.MapMouseState.ZOOM_OUT:
                    this.map.zoomOut();
                    break;

                case L.ICT.MapMouseState.POINT:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.point(callback, context);
                    break;

                case L.ICT.MapMouseState.POLYLINE:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.polyline(callback, context);
                    break;

                case L.ICT.MapMouseState.PATH:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.path(callback, context);
                    break;

                case L.ICT.MapMouseState.CIRCLE:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.circle(callback, context);
                    break;

                case L.ICT.MapMouseState.RECTANGLE:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.rectangle(callback, context);
                    break;

                case L.ICT.MapMouseState.POLYGON:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.polygon(callback, context);
                    break;
                case L.ICT.MapMouseState.SECTOR:
                    if (this._drawTool == null)
                        this._drawTool = new L.ICT.Draw(this.map);
                    this._drawTool.sector(callback, context);
                    break;
                case L.ICT.MapMouseState.MEASURELEN:
                    ;
                    break;

                case L.ICT.MapMouseState.MEASUREAREA:
                    ;
                    break;

                default:
                    ;
                    break;
            }
            if (precall != null) {
                precall();
            }
            this.status = type;
        },

        /**
         *重置鼠标状态
         *@method deactivate
         */
        deactivate: function() {
            if (this._drawTool) this._drawTool.disable();
            //清除地图事件
            //....
            //重置鼠标状态
            this.setCursor(L.ICT.MapMouseState.PAN);
        },

        /*设置鼠标状态*/
        setCursor: function(type) {
            if (type == L.ICT.MapMouseState.PAN) {
                this.map.getContainer().style.cursor = "";
            } else {
                this.map.getContainer().style.cursor = "default";
            }
        },

        /*设置鼠标状态样式*/
        setCursorImg: function(cursorImg) {
            if (cursorImg != undefined)
                this.map.getContainer().style.cursor = "url(themes/images/cursor/" + cursorImg + "),auto";
            else
                this.map.getContainer().style.cursor = "";
        },

        //获取底图
        getBaseLayer: function() {
            return this._baseLayer;
        },

        //设置底图
        setBaseLayer: function(baselayer) {
            this._baseLayer = baselayer;
        },

        //获取地图对象
        getMap: function() {
            return this.map;
        },

        getCurZoom: function() {
            return this.map.getZoom();
        },

        getMinZoom: function() {
            return this.map.getMinZoom();
        },

        getMaxZoom: function() {
            return this.map.getMaxZoom();
        },

        getBounds: function() {
            return this.map.getBounds();
        },

        getBoundsExtend: function() {
            return this.map.getBounds().pad(Project_ParamConfig.RealTargetConfig.bufferRatio);
        },

        hasPoppanel: function() {
            if (this.map.getPane("popupPane").innerHTML == "") {
                return false;
            } else {
                return true;
            }
        },

        closePoppanel: function() {
            if (this.hasPoppanel) {
                this.map.closePopup();
            }

        },

        locateToArea: function(areaName) {
            switch (areaName) {
                case "DH":
                    var sw = L.latLng(25, 119);
                    var ne = L.latLng(37, 126);
                    var bounds = L.latLngBounds(sw, ne);
                    break;
                case "DN":
                    var sw = L.latLng(21, 113);
                    var ne = L.latLng(30, 126);
                    var bounds = L.latLngBounds(sw, ne);
                    break;
                case "NH":
                    var sw = L.latLng(14, 110);
                    var ne = L.latLng(20, 120);
                    var bounds = L.latLngBounds(sw, ne);
                    break;
                case "NS":
                    var sw = L.latLng(5, 109);
                    var ne = L.latLng(13, 119);
                    var bounds = L.latLngBounds(sw, ne);
                    break;
            }
            this.map.fitBounds(bounds);
        }

    });

    /*鼠标状态*/
    L.ICT.MapMouseState = {

        PAN: 'pan',

        ZOOM_IN: 'zoom_in',

        ZOOM_OUT: 'zoom_out',

        POINT: 'point',

        PATH: 'path',

        POLYLINE: 'polyline',

        CIRCLE: 'circle',

        RECTANGLE: 'rectangle',

        POLYGON: "polygon",

        MEASURELEN: "measurelen",

        MEASUREAREA: 'meausrearea',

        SECTOR: 'sector'

    };

    /*船舶图标*/
    L.ICT.ShipIcon = {

        ship1: L.icon({
            iconUrl: 'themes/images/shipIcons/target_1.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship2: L.icon({
            iconUrl: 'themes/images/shipIcons/target_2.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship3: L.icon({
            iconUrl: 'themes/images/shipIcons/target_3.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship4: L.icon({
            iconUrl: 'themes/images/shipIcons/target_4.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship5: L.icon({
            iconUrl: 'themes/images/shipIcons/target_5.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship6: L.icon({
            iconUrl: 'themes/images/shipIcons/target_6.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship7: L.icon({
            iconUrl: 'themes/images/shipIcons/target_7.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        }),

        ship8: L.icon({
            iconUrl: 'themes/images/shipIcons/target_8.png',
            iconSize: [10, 22], //图标的大小，格式，第一个参数是宽度，第二个参数是高度
            iconAnchor: [5, 11] //图标显示位置，例如宽度和高度是图标大小的一半，则经纬度的点正好在图标的中心点
        })
    };

});
